package com.snail.controller;

import com.snail.VO.PayStatusVo;
import com.snail.VO.ResultVO;
import com.snail.dto.OrderDto;
import com.snail.service.BuyerService;
import com.snail.service.OrderService;
import com.snail.utils.ResultVOUtil;
import com.sun.corba.se.spi.ior.ObjectKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @Author 【swg】.
 * @Date 2017/12/10 14:42
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@Controller
@RequestMapping("/pay")
@Slf4j
public class PayController {
    @Autowired
    private BuyerService buyerService;

    @RequestMapping("/create")
    public ModelAndView create(@RequestParam("orderId") String orderId,
                               @RequestParam("returnUrl") String returnUrl,
                               Map<String, Object> map){
        System.out.println("orderId==="+orderId);
        System.out.println("returnUrl="+returnUrl);
        map.put("status","succ");
        map.put("returnUrl",returnUrl);
        ModelAndView mv = new ModelAndView("pay/create",map);
        return mv;
    }

    //支付订单
    @GetMapping("/paid")
    public ModelAndView paid(@RequestParam("orderId") String orderId,
                             @RequestParam("openid") String openid,
                             Map<String,Object> map) {
        OrderDto orderDto = buyerService.payOrder(openid,orderId);
        PayStatusVo payStatusVo = new PayStatusVo();
        payStatusVo.setOrderId(orderDto.getOrderId());
        payStatusVo.setOrderAmount(orderDto.getOrderAmount());
        payStatusVo.setCreateTime(orderDto.getCreateTime());
        payStatusVo.setOrderStatus(orderDto.getOrderStatus());
        payStatusVo.setPayStatus(orderDto.getPayStatus());
        map.put("payStatusVo",payStatusVo);
        return new ModelAndView("/pay/paySuccess",map);
    }

}
